package crivia.ecp.carry.em;

import crivia.ecp.carry.html.X;
import crivia.ecp.carry.html.form.Input;
import crivia.ecp.common.ECR;
import crivia.ecp.i.DI;
import crivia.time.carry.Day;
import crivia.time.exception.WrongTimeFormat;

public class DayInput implements DI {
	
	public DayInput(){}
	
	public DayInput(String key,String value){
		this.key = key;
		try {
			setValue(value);
		} catch (WrongTimeFormat e) {}
	}
	
	public DayInput(String key,int year,int month,int day){
		this.key = key;
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	private String key;
	public String getKey() {
		return key;
	}
	public DayInput setKey(String key) {
		this.key = key;
		return this;
	}
	
	private String scriptKey;
	public String getScriptKey() {
		if (!ECR.hasCondition(scriptKey)){
			scriptKey = key;
		}
		return scriptKey;
	}
	public DayInput setScriptKey(String scriptKey) {
		this.scriptKey = scriptKey;
		return this;
	}
	
	private int year;
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	
	private int month;
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	
	private int day;
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}

	@Override
	public String getValue() {
		return ""+new Day(year, month, day);
	}
	
	@Override
	public DayInput setValue(String dayString) {
		Day d = Day.toDay(dayString);
		year = d.getYear();
		month = d.getMonth();
		day = d.getDay();
		return this;
	}
	
	@Override
	public String getText() {
		return getValue();
	}
	
	@Override
	public String toString() {
		String jsKey = ECR.ns(getScriptKey())
				.replaceAll("[.]", "_")
				.replaceAll("-", "_");
		String vok = "_di_"+jsKey;
		String vok2 = "_did_"+jsKey;
		return ""
			+new Input(key+"-year", ""+year, null)
				.setScriptKey(jsKey+"-year")
				.addStyle("width", "37px")
				.addAttr("onfocus", "jQuery(this).select();")
				.addAttr("onblur", "window._di_"+jsKey+"=undefined;")
			+"-"+new Input(key+"-month", ""+month, null)
				.setScriptKey(jsKey+"-month")
				.addStyle("width", "19px")
				.addAttr("onfocus", "jQuery(this).select();")
				.addAttr("onblur", "window."+vok+"=undefined;")
			+"-"+new Input(key+"-day", ""+day, null)
				.setScriptKey(jsKey+"-day")
				.addStyle("width", "23px")
				.addAttr("onfocus", "jQuery(this).select();")
				.addAttr("onblur", "window._di_"+jsKey+"=undefined;")
			+X.JavaSctipt(""
+"\n	window.setInterval(function(){"
+"\n		if ('object' != typeof window."+vok+"){"
+"\n			window."+vok+" = {};"
+"\n			return;"
+"\n		};"
+"\n		var v = function(k,v){"
+"\n			var nv = ''+v;"
+"\n			var ov = document.getElementById(k).value;"
+"\n			if (undefined==v){"
+"\n				return ov;"
+"\n			} else if (ov != nv){"
+"\n				document.getElementById(k).value = nv;"
+"\n			}"
+"\n		};"
+"\n		var o = {"
+"\n			year : v('"+jsKey+"-year')"
+"\n			,month : v('"+jsKey+"-month')"
+"\n			,day : v('"+jsKey+"-day')"
+"\n		};"
+"\n		if (true"
+"\n		&& window."+vok+".year == o.year"
+"\n		&& window."+vok+".month == o.month"
+"\n		&& window."+vok+".day == o.day){"
+"\n			return;"
+"\n		}"
+"\n		var nd = _T.toDay(o.year+'-'+o.month+'-'+o.day);"
+"\n		if (!nd && 'object'==typeof window."+vok2+"){"
+"\n			if (window."+vok+".year&&window."+vok+".year != o.year){"
+"\n				if (''==o.year"
+"\n				&& document.activeElement==document.getElementById('"+jsKey+"-year')){"
+"\n					return;"
+"\n				} else {"
+"\n					var nd = _T.toDay(window."+vok2+".year+'-'+o.month+'-'+o.day);"
+"\n				}"
+"\n			} else if (window."+vok+".month&&window."+vok+".month != o.month){"
+"\n				if ((''==o.month||'0'==o.month)"
+"\n				&& document.activeElement==document.getElementById('"+jsKey+"-month')){"
+"\n					return;"
+"\n				} else {"
+"\n					var nd = _T.toDay(o.year+'-'+window."+vok2+".month+'-'+o.day);"
+"\n				}"
+"\n			} else {"
+"\n				if ((''==o.day||'0'==o.day)"
+"\n				&& document.activeElement==document.getElementById('"+jsKey+"-day')){"
+"\n					return;"
+"\n				} else {"
+"\n					var nd = _T.toDay(o.year+'-'+o.month+'-'+window."+vok2+".day);"
+"\n				}"
+"\n			}"
+"\n			if (!nd){"
+"\n				var nd = _T.toDay("
+"\nwindow."+vok2+".year+'-'+window."+vok2+".month+'-'+window."+vok2+".day);"
+"\n			}"
+"\n		}"
+"\n		if (!nd){ nd = _T.today(); }"
+"\n		window."+vok2+" = {"
+"\n			year : nd.year"
+"\n			,month : nd.month"
+"\n			,day : nd.day"
+"\n		}"
+"\n		v('"+jsKey+"-year', nd.year);"
+"\n		v('"+jsKey+"-month', nd.month>9?nd.month:'0'+nd.month);"
+"\n		v('"+jsKey+"-day', nd.day>9?nd.day:'0'+nd.day);"
+"\n		if (window."+vok+".year&&window."+vok+".year != o.year){"
+"\n			if (nd.year > 999){"
+"\n				document.getElementById('"+jsKey+"-month').focus();"
+"\n			}"
+"\n		} else if (window."+vok+".month&&window."+vok+".month != o.month){"
+"\n			if (nd.month > 2){"
+"\n				document.getElementById('"+jsKey+"-day').focus();"
+"\n			}"
+"\n		}"
+"\n		window."+vok+" = o;"
+"\n	} , 70);"
+"\n	window.document.onkeydown = function(o){"
+"\n		if (false"
+"\n		||'object'!=typeof window._dii_"
+"\n		||8!=o.keyCode){"
+"\n			return;"
+"\n		}"
+"\n		for (var k = 0; k < window._dii_.length; k++){"
+"\n			var dii = window._dii_[k];"
+"\n			if (true"
+"\n			&&document.activeElement==dii.day"
+"\n			&&''==dii.day.value){"
+"\n				dii.month.focus();"
+"\n			} else if (true"
+"\n			&&document.activeElement==dii.month"
+"\n			&&''==dii.month.value){"
+"\n				dii.year.focus();"
+"\n			}"
+"\n		}"
+"\n	};"
+"\n	if ('object'!=typeof window._dii_) {"
+"\n		window._dii_ = [];"
+"\n	}"
+"\n	window._dii_[_dii_.length] = {"
+"\n		year : document.getElementById('"+jsKey+"-year')"
+"\n		,month : document.getElementById('"+jsKey+"-month')"
+"\n		,day : document.getElementById('"+jsKey+"-day')"
+"\n	};"
			);
	}

}
